import Base from './base'
import Storage from "../storage";
import wepy from "wepy";
import ApiUrl from "../../apiUrl";
const Get = async (url, params, options = {}) => await Base.doRequest(url, 'GET', params, options)
const Post = async (url, params, options = {}) => await Base.doRequest(url, 'Post', params, options)
const Upload = async (filePath, params = {}) => await Base.wxUpload(`upload`, filePath, params)

const GetBasic = (url, params, options = {}, callback) => Base.doRequest(url, 'GET', params, options, callback)
const PostBasic = (url, params, options = {}, callback) => Base.doRequest(url, 'Post', params, options, callback)

const WX = (url, params, callback) => {
  wx.request({
    url: `${url}`,
    data: params,
    success: (data) => callback(data)
  });
};

// 判断是否登录
const isLogin = async () => {
  const res = await wepy.request({
    url: ApiUrl.user.isLogin,
    method: 'GET',
    header: { 'X-Access-Token': Storage.get(Storage.key.loginToken) }
  });
  const code = res.data.code;
    if (1 === code) {

      // 已经登录了
      Storage.set(Storage.key.alreadyLogin, true);
    } else if (2 === code) {

      // 未登录/过期
      Storage.set(Storage.key.alreadyLogin, false);
    }
  return code;
};

export default {
  Get,
  Post,
  PostBasic,
  GetBasic,
  WX,
  Upload,
  isLogin,
  ApiUrl
}
